<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;

class Index extends Controller
{
    public function index(){
        $action = Request::instance()->get();
        //如果不根据第一个GET内容 获取内容  就要注意这个地方
        $keys = array_keys($action);
        $method = isset($keys[0]) ? $keys[0] : 'VRGetVideoClass';

        //加2个判断  防止出现错误
        if (!$method){
            die("error action");
        }

        if (!method_exists($this,$method)){
            die("error method");
        }
        return $this->$method();
    }

    //轮播图
    public function VRWebConfig(){
        $web_config = Request::instance()->get('VRWebConfig');
        $sel = Request::instance()->get('sel');
        if($web_config==1&&$sel==0){
            $carousel = new \app\index\model\Carousel();
            $carousel_res = $carousel->getCarouselFigure();

            for($i = 1;$i<=count($carousel_res);$i++){
                $dataSource[] = [
                    "videoimg$i"  => $carousel_res[$i-1]['img'],
                    'video'.$i.'src' => $carousel_res[$i-1]['videosrc'],
                ];
            }
            return toXml(1 ,'errors',$dataSource);
        }

    }

    // 视频类别
    public function VRGetVideoClass(){
        $vr_get_video_class = Request::instance()->get('VRGetVideoClass');
        if($vr_get_video_class==1){
            $video_class = new \app\index\model\VideoClass();
            $video_class_res = $video_class->getVideoClass();
            foreach ($video_class_res as $k1){
                $dataSource[] = [
                    'classtype' => $k1['classtype'],
                    'classimg1' => $k1['classimg1'],
                    'havenum' => $k1['havenum'],
                ];
            }
            return toXml(1,'errors',$dataSource);
        }
    }

    /**
     * 精选推荐视频
     */
    public function VRLoveVidApp(){
        //VRLoveVidApp=1&iPage=0
        // 获取页数,强制转换为整型
        $page = (int)Request::instance()->get('iPage');
        // 如果$page不存在,则设置为1
        $page = isset($page) ? $page : 1;
        // 如果$page小于0,设置为1
        $page = $page <= 0 ? 1 : $page;

        // 获取到精选视频总数,除以每页视频数,得出总页数,防止$page大于总页数
        $vr_love_vid = new \app\index\model\VideoList();
        $vr_love_vid_count = $vr_love_vid->getVideoListCount('精选');

        $page_count = ceil($vr_love_vid_count/6);

        $page = $page > $page_count ? $page_count : $page;

        $video_vid_res = $vr_love_vid->getLoveVidApp($page);

        // 当下一页没有数据时,返回0和一个空数组(Table2不会出现)
        if(empty($video_vid_res)){
            return toXml(0 ,$video_vid_res);
        }

        foreach($video_vid_res as $k1){
            $dataSource[] = [
                'vID' => $k1['vid'],
                'address' => $k1['address'],
                'classtype' => $k1['classtype'],
                'title' => $k1['title'],
                'body1' => $k1['body1'],
                'playnum' => $k1['playnum'],
                'imgaddr' => $k1['imgaddr'],
                'randompath' => $k1['randompath'],
                'upclient' => $k1['upclient'],
                'shell' => $k1['shell'],
            ];
        }
        return toXml(1,'errors',$dataSource);
    }

    //视频列表
    public function VRGetList(){
        //VRGetList=1&activeopenid=&classtype=云侦探&iPage=0
        $vr_get_list = Request::instance()->get('VRGetList');
        $page = (int)Request::instance()->get('iPage');
        $class_type = Request::instance()->get('classtype');
        $active_openid = (int)Request::instance()->get('activeopenid');//不知道是什么

        if($vr_get_list==1){
            // 如果$page不存在,则设置为1
            $page = isset($page) ? $page : 1;
            // 如果$page小于0,设置为1
            $page = $page <= 0 ? 1 : $page;
            // 获取某一类视频总数,除以每页视频数,得出总页数,防止$page大于总页数
            $video_list = new \app\index\model\VideoList();
            $video_list_count = $video_list->getVideoListCount($class_type);
            $page_count = ceil($video_list_count/10);
            $page = $page > $page_count ? $page_count : $page;

            $video_list_res = $video_list->getVideoList($page,$class_type);
            if(empty($video_list_res)){
                return toXml(0,'errors');
            }
            $comments = new \app\index\model\Comments();
            foreach($video_list_res as $k1){
                $comments_count = $comments->getCommentsListCount($k1['vid']);
                $dataSource[] = [
                    'vID' => $k1['vid'],
                    'shell' => $k1['shell'],
                    'address' => $k1['address'],
                    'uptime' => $k1['uptime'],
                    'classtype' => $k1['classtype'],
                    'title' => $k1['title'],
                    'body1' => $k1['body1'],
                    'playnum' => $k1['playnum'],
                    'imgaddr' => $k1['imgaddr'],
                    'upclient' => $k1['upclient'],
                    'randompath' => $k1['randompath'],
                    'vurl' => $k1['vurl'],
                    'ratedtotall' => $comments_count,
                    'xzid' => $k1['xzid'],
                    'lxjid' => $k1['lxjid'],
                    'jishunumber' => $k1['jishunumber'],
                    'shoucangnum' => $k1['shoucangnum'],
                ];
            }
            return toXml(1,'errors',$dataSource);
        }
    }

    // 历史视频
    public function VRHistory_Get(){
        //VRHistory_Get=1&unionid=13411769330&token=142904
        $vr_history_get = Request::instance()->get('VRHistory_Get');
        $unionid = Request::instance()->get('unionid');

        if($vr_history_get==1){
            $user = new \app\index\model\User();
            $user_info = $user->getUserInfo('tel',$unionid);
            //查不到该用户信息时返回空数组
            if(empty($user_info)){
                return toXml(0 ,'history');
            }

            //用户的播放记录
            $watch_history = new \app\index\model\UserWatchHistory();
            $watch_history_res = $watch_history->getWatchHistory($user_info['id']);
            //该用户没有历史记录返回空数组
            if(empty($watch_history_res)){
                return toXml(0 ,'history');
            }

            $video_list = new \app\index\model\VideoList();
            foreach($watch_history_res as $k1){
                //获取视频信息
                $video_info[] = $video_list->getOneVideo($k1['vid']);
            }
            foreach($video_info as $k2){
                $dataSource[] = [
                    'imgaddr' => $k2['imgaddr'],
                    'title' => $k2['title'],
                    'shell' => $k2['shell'],
                    'body1' => $k2['body1'],
                    'playnum' => $k2['playnum'],
                    'vID' => $k2['vid'],
                ];
            }
            return toXml(1 ,'history',$dataSource);
        }
    }

    //用户反馈
    public function SendYJ(){
        //SendYJ=1&comment=gggggg&unionid=13411769330&token=877321
        $send_yj = Request::instance()->get('SendYJ');
        $comment = Request::instance()->get('comment');

        if($send_yj==1){
            //取出该手机的用户ID
            $user = new \app\index\model\User();
            $user_info = $user->getUserInfo('tel',Request::instance()->get('unionid'));
            if(empty($user_info)){
                return toXml(3,'feedback');
            }
            if($comment==''){
                return toXml(2,'feedback');
            }
            //插入feedback表
            $feedback = new \app\index\model\Feedback();
            $comment = htmlentities($comment);
            $feedback_res = $feedback->putFeedbackContent(trim($comment),$user_info['id']);
            if($feedback_res==0){
                return toXml($feedback_res,'feedback');
            }
           return toXml(1,'feedback');
        }

    }

    //用户登录
    public function VRLogin(){
        //VRLogin=1&tel=13411769330&password=qwerty
        $tel = Request::instance()->post('tel');
        $pass = Request::instance()->post('password');
        $user = new \app\index\model\User();
        $login_res = $user->toCheckLogin($tel,$pass);
        $value = rand(1,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);

        if($login_res['status']){
            $user_info = $user->getUserInfo('tel',$tel);
            $dataSource[] = [
                'UnionID' => $user_info['UnionID'],
                'Level' => $user_info['level'],
                'LockMoney' => $user_info['lockmoney'],
                'tjmoney' => $user_info['tjmoney'],
                'password' => null,
                'WXUserName' => $user_info['wxusername'],
                'WXHeadImg' => $user_info['wxheadimg'],
                'HaveGuanZhu' => $user_info['haveguanzhu'],
                'tel' => $user_info['tel'],
                'havemoney' => $user_info['havemoney'],
                'money' => $user_info['money'],
                'shell' => $user_info['shell'],
                'todayplaynum' => $user_info['todayplaynum'],
                'maxtodayplaynum' => $user_info['maxtodayplaynum'],
                'maxtodayzhuanfa' => $user_info['maxtodayzhuanfa'],
                'todayzhuanfa' => $user_info['todayzhuanfa'],
                'jifen' => $user_info['jifen'],
                'xiaxian' => $user_info['xiaxian'],
                'id' => $user_info['id'],
                'key' => $user_info['key'],
                'url' =>"userinfo.aspx?id=".$user_info['id']."&amp;classkey1=VR%e5%b0%8f%e4%b8%bb&amp;classkey2=%e7%bd%91%e5%8f%8b&amp;mUnionID=".$user_info['UnionID']."&amp;t=".$user_info['tel'],
            ];
            Session::set('user_info',$dataSource);
            return toXml($login_res['status'],'login',$dataSource,$tel,$value);
        }else{
            return toXml($login_res['status'],'login',$dataSource=[],0,'');
        }
    }

    //修改密码
    public function changePW(){
        //changepw=1&tel=13411769330&oldpassword=qwerty&newpassword=qwerty
        $tel = Request::instance()->post('tel');
        $oldpassword = Request::instance()->post('oldpassword');
        $newpassword = Request::instance()->post('newpassword');
        $user = new \app\index\model\User();
        $change_password_res = $user->changePassword($tel,$oldpassword,$newpassword);
        return toXml($change_password_res,'changpw');
    }

    //检查手机是否注册
    public function checkTel(){
        // checktel=1&tel=15625052273
        $checktel = Request::instance()->get('checktel');
        $tel = Request::instance()->get('tel');
        if($checktel==1){
            $user = new \app\index\model\User();
            $check_tel_res = $user->getUserInfo('tel',$tel);
            if(!empty($check_tel_res)){
                return toXml(0,'code');
            }
            return toXml(1,'code');
        }
    }

    //视频详细信息
    public function VRGetVideoFromVid(){
        //VRGetVideoFromVid=1&vid=799&unionid=13411769330&token=877321
        $vr_get_video_from_vid = Request::instance()->get('VRGetVideoFromVid');
        $vid = Request::instance()->get('vid');

        if($vr_get_video_from_vid==1){
            $video_list = new \app\index\model\VideoList();
            $video_info[] = $video_list->getOneVideo($vid);
            if(empty($video_info[0])){
                return toXml(0 ,'errors');
            }

            foreach($video_info as $k1){
                $dataSource[] = [
                    'shell' => $k1['shell'],
                    'vID' => $k1['vid'],
                    'address' => $k1['address'],
                    'uptime' => $k1['uptime'],
                    'classtype' => $k1['classtype'],
                    'title' => $k1['title'],
                    'body1' => $k1['body1'],
                    'playnum' => $k1['playnum'],
                    'imgaddr' => $k1['imgaddr'],
                    'upclient' => $k1['upclient'],
                    'randompath' => $k1['randompath'],
                    'vurl' => $k1['vurl'],
                    'ratedtotall' => $k1['ratedtotall'],
                    'xzid' => $k1['xzid'],
                    'lxjid' => $k1['lxjid'],
                    'jishunumber' => $k1['jishunumber'],
                ];
            }
            //搜索该视频类别，列出播放次数最多的4个该类视频，拼接为$table3
            $recommended_res = $video_list->getRecommendedVideo($k1['classtype']);
            foreach($recommended_res as $k2){
                $table3[] = [
                    'shell' => $k2['shell'],
                    'vID' => $k2['vid'],
                    'address' => $k2['address'],
                    'uptime' => $k2['uptime'],
                    'classtype' => $k2['classtype'],
                    'title' => $k2['title'],
                    'body1' => $k2['body1'],
                    'playnum' => $k2['playnum'],
                    'imgaddr' => $k2['imgaddr'],
                    'upclient' => $k2['upclient'],
                    'randompath' => $k2['randompath'],
                    'vurl' => $k2['vurl'],
                    'ratedtotall' => $k2['ratedtotall'],
                    'xzid' => $k2['xzid'],
                    'lxjid' => $k2['lxjid'],
                    'jishunumber' => $k2['jishunumber'],
                ];
            }

            return toXml(1,'errors',$dataSource,$HaveSecondRet=0,$value=null,$table3);
        }
    }

    // 获取视频评论
    public function VRGetRated(){
        //VRGetRated=1&vID=849&iPage=0
        $vid = Request::instance()->get('vID');
        $page = Request::instance()->get('iPage');
        // 如果$page不存在,则设置为1
        $page = isset($page) ? $page : 1;
        // 如果$page小于0,设置为1
        $page = $page <= 0 ? 1 : $page;
        // 获取评论总数,除以每页评论数,得出总页数,防止$page大于总页数
        $comments = new \app\index\model\Comments();
        $comments_count = $comments->getCommentsListCount($vid);
        $page_count = ceil($comments_count/20);
        $page = $page > $page_count ? $page_count : $page;

        $comments_res = $comments->getCommentsList($vid,$page);

        if(empty($comments_res)){
            return toXml(0,'comments');
        }
        foreach($comments_res as $k1){
            //评论人的信息
            $user = new \app\index\model\User();
            $user_info = $user->getUserInfo('id',$k1['uid']);

            $dataSource[] = [
                'pSN' => $k1['id'],
                'vID' => $k1['vid'],
                'UnionID' => $user_info['tel'],
                'comment' => $k1['contents'],
                'ratedtime' => date('Y-m-d H:i:s',$k1['commentstime']),
                'rated_type' => 2,
                'click' => 0,
                'reply' => 0,
                'img1' => $user_info['image'],
                'UserName' => $user_info['name'],
                'id' => $user_info['id'],
            ];
        }
        return toXML(1,'comments',$dataSource);
    }

    // 写评论
    public function VRRated(){
        //VRRated=1&vID=62&comment=gggggg&unionid=13411769330&token=765422
        $vr_rated = Request::instance()->get('VRRated');
        $vid = Request::instance()->get('vID');
        $comment = Request::instance()->get('comment');
        $unionid = Request::instance()->get('unionid');
        $token = Request::instance()->get('token');
        if($vr_rated==1&&$comment!=''){
            $user = new \app\index\model\User();
            $user_info = $user->getUserInfo('tel',$unionid);
            //如果UID不存在，返回0
            if(empty($user_info)){
                return toXml(3,'putcommetns');
            }

            $comments = new \app\index\model\Comments();
            $comment = htmlentities($comment);
            $put_comments_res = $comments->putComments($vid,$comment,$user_info['id']);
            if(!$put_comments_res){
                return toXml(3,'putcommetns');
            }
          return toXml(1,'putcommetns',$dataSource=[],$comment,$put_comments_res);
        }
        return toXml(2,'putcommetns');

    }

    //视频播放次数
    public function VRSetPlayNum(){
        //VRSetPlayNum=1&vID=787&unionid=13411769330&token=142904
        $vr_set_playnum = Request::instance()->get('VRSetPlayNum');
        $vid = Request::instance()->get('vID');
        $unionid = Request::instance()->get('unionid');
        if($vr_set_playnum==1){
            $video_list=new \app\index\model\VideoList();
            $video_info = $video_list->getOneVideo($vid);
            //该视频存在时再往下执行
            if(!empty($video_info)){
                $user = new \app\index\model\User();
                $user_info = $user->getUserInfo('tel',$unionid);

                //用户存在时往下执行
                if(!empty($user_info)){
                    //增加播放次数
                    $video_list->addVideoPlayNum($vid);
                    //放进个人播放记录
                    //登录做完后，改用session里的id传值
                    $watch_history = new \app\index\model\UserWatchHistory();
                    $watch_history->putWatchHistory($user_info['id'],$vid);
                    return toXml(1,'putcommetns',$dataSource=[],$video_info['address']);
                }
            }
            return toXml(0,'errors');
        }
        return toXml(0,'errors');
    }

    //搜索
    public function FindData(){
        //FindData=1&key=vr&selClass=0&ipage=0
        $find_data = Request::instance()->get('FindData');
        $key = Request::instance()->get('key');
        $page = Request::instance()->get('ipage');
        $sel_class = Request::instance()->get('selClass');

        // 如果$page不存在,则设置为1
        $page = isset($page) ? $page : 1;
        // 如果$page小于0,设置为1
        $page = $page <= 0 ? 1 : $page;

        if($find_data==1&&$sel_class==0&&$key!=''){
            $key = htmlentities($key);
            $search_list = new \app\index\model\VideoList();
            $search_list_count = $search_list->getSearchListCount(trim($key));
            $page_count = ceil($search_list_count/6);
            $page = $page > $page_count ? $page_count : $page;
            $search_list_res = $search_list->getSearchList(trim($key),$page);
            if(!empty($search_list_res)){
                foreach($search_list_res as $k1){
                    $dataSource[] = [
                        'vID' => $k1['vid'],
                        'shell' => $k1['shell'],
                        'classtype' => $k1['classtype'],
                        'title' => $k1['title'],
                        'body1' => $k1['body1'],
                        'playnum' => $k1['playnum'],
                        'imgaddr' => $k1['imgaddr'],
                    ];
                }
                return toXml(1,'errors',$dataSource);
            }
        }
        return toXml(0,'errors');
    }

    //签到
    public function VRAddQiandao(){
        //VRAddQiandao=1&unionid=13411769330&token=266409
        $vr_add_qiandao = Request::instance()->get('VRAddQiandao');
        $unionid = Request::instance()->get('unionid');
        if($vr_add_qiandao==1){
            $user = new \app\index\model\User();
            $user_info = $user->getUserInfo('tel',$unionid);
            if(!empty($user_info)){
                $user_sign_in = new \app\index\model\SignIn();
                $user_sign_in->addSignIn($user_info['id']);
                return toXml(0,'signin');
            }
        }
    }

    //注册
    public function AppOrWebReg(){
        // 短信验证码通过后才往下执行
        $tel = trim(Request::instance()->post('tel'));
        $password = trim(Request::instance()->post('password'));

        $user = new \app\index\model\User();
        $register_res = $user->Register($tel,$password);

        if($register_res){
            return toXml(1,'errors');
        }else{
            return toXml(0,'code');
        }
    }
    public function test(){
        return $this->fetch();
    }


    //修改个人资料
    public function ChangeUserInfo(){
        //ChangeUserInfo=1&strUserName=13411769330&strHeadImg=http://www.vrmkj.com/v/uploadimages/20170309/dc7f0cf0-d524-4be7-866b-4f9cf2c7360e.jpg&unionid=13411769330&token=439799&say=&hangye=&img2=&img3=&img4=&img5=&img6=&img7=

        // 头像图片上传和用户名修改
    }

    //商品 接口暂停开发
//    public function VRGetP (){
//        // 获取页数,强制转换为整型
//        $page = (int)Request::instance()->get('page');
//
//        // 如果$page不存在,则设置为1
//        $page = isset($page) ? $page : 1;
//
//        // 如果$page小于0,设置为1
//        $page = $page <= 0 ? 1 : $page;
//
//        // 获取到商品总数,除以每页商品数,得出总页数,防止$page大于总页数
//        $goods = Model('Goods');
//        $goods_count = $goods->getGoodsCount();
//        $page_count = ceil($goods_count/4);
//        $page = $page > $page_count ? $page_count : $page;
//
//        $goods_res = $goods->getGoodsList($page);
//        // 当下一页没有数据时,返回0和一个空数组(Table2不会出现)
//        if(empty($goods_res)){
//            return toXml(0 ,$goods_res);
//        }
//
//        foreach($goods_res as $k1){
//            $goods_img = Model('goods_img');
//            $goods_img_res = $goods_img->getGoodsImg($k1['pid']);
//
//            $dataSource[] = [
//                'pid' => $k1['pid'],
//                'name' => $k1['name'],
//                'kucun' => $k1['kucun'],
//                'zongliang' => $k1['zongliang'],
//                'say' => $k1['say'],
//                'img1' => $goods_img_res[0]['url'],
//                'img2' => $goods_img_res[1]['url'],
//                'jiage' => $k1['jiage'],
//                'img3' => $goods_img_res[2]['url'],
//                'yuanjia' => $k1['yuanjia'],
//                'level' => $k1['level'],
//                'bshowonshop' => $k1['bshowonshop'],
//                'classtype' => $k1['classtype'],
//                'canshu' => '&lt;div style=" text-align:left; font-size:16px; color:#666666; padding:10px;padding-left:20px;padding-top:0px; display:inline-block; width:330px; "&gt; &lt;p&gt;商品名称：春云早大红袍200g&lt;/p&gt; &lt;p style=" padding-bottom:0px; margin-bottom:0px;"&gt;商品毛重：0.63kg&lt;/p&gt; &lt;/div&gt; &lt;div style=" text-align:left; font-size:16px; color:#666666; padding:10px;padding-left:20px;padding-top:0px; display:inline-block; width:330px; "&gt; &lt;p&gt;商品产地：福建省武夷山市&lt;/p&gt; &lt;p style=" padding-bottom:0px; margin-bottom:0px;"&gt;包装：独立包装&lt;/p&gt; &lt;/div&gt; &lt;div style=" text-align:left; font-size:16px; color:#666666; padding:10px;padding-left:20px;padding-top:0px; display:inline-block; width:330px; "&gt; &lt;p&gt;工艺：工夫红茶&lt;/p&gt; &lt;p&gt;等级：一级 &lt;/p&gt; &lt;/div&gt; &lt;div style=" text-align:left; font-size:16px; color:#666666; padding:10px;padding-left:20px;padding-top:0px; display:inline-block; width:330px; "&gt; &lt;p&gt;包装单位：罐装&lt;/p&gt; &lt;p style=" padding-bottom:0px; margin-bottom:0px;"&gt;净含量：200g&lt;/p&gt; &lt;/div&gt; &lt;div style=" text-align:left; font-size:16px; color:#666666; padding:10px;padding-left:20px;padding-top:0px; display:inline-block; width:330px; "&gt; &lt;p&gt;保质期：720天 &lt;/p&gt; &lt;p&gt;产品标准号：GB/18745-2006&lt;/p&gt; &lt;/div&gt;',
//                'newtime' => date('Y_m_d H:i:s',$k1['newtime']),
//                'zhibao' => $k1['zhibao'],
//                'youfei' => $k1['youfei'],
//                'playnum' => $k1['playnum'],
//                'pinglun' => '0',
//                'img4' => $goods_img_res[3]['url'],
//                'img5' => $goods_img_res[4]['url'],
//                'img6' => $goods_img_res[5]['url'],
//                'img7' => $goods_img_res[6]['url'],
//                'yongjin' => $k1['yongjin'],
//
//            ];
//        }
//        return toXml(1 ,$dataSource);
//    }
}
